#include <TGS/MapControl2.0/MapWidget.h>
#include <TGS/GeoMath2.0/Envelope.h>
#include "MapCanvas.h"

namespace TGS{
namespace MapControl{

struct MapWidget::MapWidgetPrivate
{
	MapCanvas*	mapCanvas;
};

MapWidget::MapWidget(QWidget *parent)
	: QWidget(parent)
	, _data(new MapWidgetPrivate())
{
	_data->mapCanvas = new MapCanvas(this);
	connect(_data->mapCanvas, SIGNAL(screenCoordinate(int, int)), 
			this, SIGNAL(screenCoordinate(int, int)));
	connect(_data->mapCanvas, SIGNAL(mapCoordinate(double, double)), 
		this, SIGNAL(mapCoordinate(double, double)));
	connect(_data->mapCanvas, SIGNAL(polygonSelected(const TGS::GeoMath::Polygon&)), 
		this, SIGNAL(polygonSelected(const TGS::GeoMath::Polygon&)));
	connect(_data->mapCanvas, SIGNAL(pointSelected(const TGS::GeoMath::Point&)), 
		this, SIGNAL(pointSelected(const TGS::GeoMath::Point&)));
	connect(_data->mapCanvas, SIGNAL(extentChanged(const TGS::GeoMath::Envelope&)), 
		this, SIGNAL(extentChanged(const TGS::GeoMath::Envelope&)));
	connect(_data->mapCanvas, SIGNAL(deselected()), this, SIGNAL(deselected()));
	connect(_data->mapCanvas, SIGNAL(mouseMoved( QMouseEvent*)), this, SIGNAL(mouseMoved( QMouseEvent*)));
	connect(_data->mapCanvas, SIGNAL(mousePressd( QMouseEvent*)), this, SIGNAL(mousePressd( QMouseEvent*)));
	connect(_data->mapCanvas, SIGNAL(mouseReleased( QMouseEvent*)), this, SIGNAL(mouseReleased( QMouseEvent*)));
	connect(_data->mapCanvas, SIGNAL(polygonDrawed(const TGS::GeoMath::Polygon&)), 
		this, SIGNAL(polygonDrawed(const TGS::GeoMath::Polygon&)));

	resize(800, 600);
}

MapWidget::~MapWidget()
{
	 
}

void MapWidget::resizeEvent( QResizeEvent * /*e*/ )
{
	_data->mapCanvas->resize(this->width(), this->height());
	emit screenSizeChanged(size());
}

inline void MapWidget::setCompassVisible( bool visible )
{
	_data->mapCanvas->setCompassVisible(visible);
}

inline void MapWidget::addRenderer( Renderer* renderer )
{
	_data->mapCanvas->addRenderer(renderer);
}

inline void MapWidget::setExtent( const TGS::GeoMath::Envelope& envelope )
{
	_data->mapCanvas->setExtent(envelope);
}

inline void MapWidget::addGraphicsItem( QGraphicsItem* item )
{
	_data->mapCanvas->addGraphicsItem(item);
}

inline void MapWidget::fullExtent()
{
	_data->mapCanvas->fullExtent();
}

inline void MapWidget::refresh()
{
	_data->mapCanvas->refresh();
}

inline void MapWidget::clearRenderers()
{
	_data->mapCanvas->clearRenderers();
}

inline MapToolBar* MapWidget::getMapToolBar()
{
	return _data->mapCanvas->getMapToolBar();
}

inline void MapWidget::setSelectRubberBandColor( const QColor& color )
{
	_data->mapCanvas->setSelectRubberBandColor(color);
}

inline void MapWidget::removeRenderer( Renderer* renderer )
{
	_data->mapCanvas->removeRenderer(renderer);
}

inline void MapWidget::setWheelAction( WheelAction action, double factor /*= 1.2 */ )
{
	_data->mapCanvas->setWheelAction(action, factor);
}

inline MapWidget::Envelope MapWidget::getExtent() const
{
	return _data->mapCanvas->extent();
}

inline void MapWidget::centerOn( const Point& point )
{
	_data->mapCanvas->centerOn(point);
}

void MapWidget::setMapToolType( MapToolType toolType )
{
	_data->mapCanvas->setMapToolType(toolType);
}

void MapWidget::setCoordinateVisible( bool visible )
{
	_data->mapCanvas->setCoordinateVisible(visible);
}

void MapWidget::setMapToolBarVisible( bool visible )
{
	_data->mapCanvas->setMapToolBarVisible(visible);
}

void MapWidget::setMapBackgroundColor( const QColor& color )
{
	_data->mapCanvas->setCanvasColor(color);
}

MapCoordinateItem* MapWidget::getMapCoordinateItem()
{
	return _data->mapCanvas->getMapCoordinateItem();
}

void MapWidget::setScaleVisible( bool visible )
{
	_data->mapCanvas->setScaleVisible(visible);
}

const RenderContext* MapWidget::getRenderContext() const
{
	return _data->mapCanvas->renderContext();
}

const QPixmap* MapWidget::getCanvasPixmap() const
{
	return &_data->mapCanvas->canvasPixmap();
}

RendererList* MapWidget::getRenderers()
{
	return _data->mapCanvas->getRenderers();
}

const RendererList* MapWidget::getRenderers() const
{
	return _data->mapCanvas->getRenderers();
}


}//MapControl
}//TGS